package com.kx.center.util;

import java.security.GeneralSecurityException;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class SHA1Coder {
	public static final String ALGORITHM = "PBKDF2WithHmacSHA1";
	public static final int ITERATION_COUNT = 8192;
	public static final int KEY_SIZE = 160;

	public static byte[] encrypt(char[] password, byte[] salt)
			throws GeneralSecurityException {
		return encrypt(password, salt, ITERATION_COUNT, KEY_SIZE);
	}

	public static byte[] encrypt(char[] password, byte[] salt,
			int iterationCount, int keySize) throws GeneralSecurityException {
		if(salt == null)
			return null;
		PBEKeySpec spec = new PBEKeySpec(password, salt, iterationCount, keySize);
		SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
		return factory.generateSecret(spec).getEncoded();
	}

}
